""" 1.遍历当目录和子目录下的头文件，从头文件中获取类名以及父类和继承方式
    2.把类名，父类名，继承方式做成一个2行3列的word表格，表格需要栅格样式
"""

import os
import re
import docx
from docx import Document
from docx.oxml.ns import nsdecls
from docx.oxml import parse_xml
from docx.shared import Pt, RGBColor


def get_class_name(content):
    """ 获取类名 """
    class_name = re.findall(r"class\s+(\w+)\s*:", content)
    class_name2 = re.findall(r"class\s+(\w+)\s*:\s*\w+\s+\w+", content)
    return class_name

def get_class_parent(content):
    """ 获取父类 """
    parent_class_name = re.findall(r"class\s+(\w+)\s*:\s*\w+\s+\w+", content)
    return parent_class_name

def get_class_parent_type(content):
    """ 获取继承方式 """
    class_parent_type = re.findall(r"class\s+\w+\s*:\s*(\w+)\s+\w+", content)
    return class_parent_type

def get_private_members(content):
    """ 获取私有成员变量 """
    private_members = re.findall(r"private:\s*(\w+\s+\w+;)", content)
    return private_members

def get_public_methods(content):
    """ 获取共有成员函数 """
    public_methods = re.findall(r"public:\s*(\w+\s+\w+\(.*\);)", content)
    return public_methods

def get_members_type(content):
    """ 获取成员变量的类型 """
    members_type = re.findall(r"(\w+)\s+\w+;", content)
    return members_type

def get_methods_params(content):
    """ 获取成员函数的参数 """
    methods_params = re.findall(r"\w+\s+\w+\((.*)\);", content)
    return methods_params

def get_methods_return(content):
    """ 获取成员函数的返回值 """
    methods_return = re.findall(r"\w+\s+(\w+)\s*\(", content)
    return methods_return

def get_methods_params_type(content):
    """ 获取成员函数的参数类型 """
    methods_params_type = re.findall(r"(\w+)\s+\w+", content)
    return methods_params_type

def get_methods_params_name(content):
    """ 获取成员函数的参数名 """
    methods_params_name = re.findall(r"\w+\s+(\w+)", content)
    return methods_params_name

def get_methods_return_type(content):
    """ 获取成员函数的返回值类型 """
    methods_return_type = re.findall(r"(\w+)\s+\w+\s*\(", content)
    return methods_return_type

def get_class_info(file):
    """ 获取类名，父类，继承方式 """
    try:
        with open(file, 'r', encoding='gb2312') as f:
            content = f.read()
            class_name = get_class_name(content)
            parent_class_name = get_class_parent(content)
            class_parent_type = get_class_parent_type(content)
            return class_name, parent_class_name, class_parent_type
    except:
        with open(file, 'r', encoding='utf-8') as f:
            content = f.read()
            class_name = get_class_name(content)
            parent_class_name = get_class_parent(content)
            class_parent_type = get_class_parent_type(content)
            return class_name, parent_class_name, class_parent_type
        
def get_members_info(file):
    """ 获取成员变量的类型，成员变量的名字 """
    with open(file, 'r', encoding='gb2312') as f:
        content = f.read()
        members_type = get_members_type(content)
        private_members = get_private_members(content)
        return members_type, private_members
    
def get_methods_info(file):
    """ 获取成员函数的参数类型，成员函数的参数名，成员函数的返回值类型 """
    with open(file, 'r', encoding='gb2312') as f:
        content = f.read()
        public_methods = get_public_methods(content)
        methods_params = get_methods_params(content)
        methods_return = get_methods_return(content)
        methods_params_type = get_methods_params_type(content)
        methods_params_name = get_methods_params_name(content)
        methods_return_type = get_methods_return_type(content)
        return public_methods, methods_params, methods_return, methods_params_type, methods_params_name, methods_return_type
    
def get_all_files(path):
    """ 获取所有头文件 """
    all_files = []
    for root, dirs, files in os.walk(path):
        for file in files:
            if file.endswith('.h'):
                all_files.append(os.path.join(root, file))
    return all_files

# 表格首行背景色设置
def tabBgColor(table,cols,colorStr):
    shading_list = locals()
    for i in range(cols):
        shading_list['shading_elm_'+str(i)] = parse_xml(r'<w:shd {} w:fill="{bgColor}"/>'.format(nsdecls('w'),bgColor = colorStr))
        table.rows[0].cells[i]._tc.get_or_add_tcPr().append(shading_list['shading_elm_'+str(i)])

def main():
    # 删除test.docx文件
    if os.path.exists("test.docx"):
        os.remove("test.docx")
    else:
        print("The test.docx does not exist")

    """ 主函数 """
    # 获取所有头文件
    all_files = get_all_files('.')
    # 创建word文档
    doc = docx.Document()
    # 设置字体

    # 遍历所有头文件
    for file in all_files:
        # 设置宋体字体，字号10，粗体
        doc.styles['Normal'].font.name = '宋体'
        doc.styles['Normal']._element.rPr.rFonts.set(docx.oxml.ns.qn('w:eastAsia'), '宋体')
        doc.styles['Normal'].font.size = docx.shared.Pt(10)
        doc.styles['Normal'].font.bold = True
        # 设置表格样式
        table = doc.add_table(rows=1, cols=3)
        table.style = 'Table Grid'
        # 设置表格标题
        table.cell(0, 0).text = '类名'
        table.cell(0, 1).text = '基类'
        table.cell(0, 2).text = '继承方式'
        colorStr = 'A6A6A6'
        tabBgColor(table, 3, colorStr)

        # 获取类名，父类，继承方式
        class_name, parent_class_name, class_parent_type = get_class_info(file)
        # 获取成员变量的类型，成员变量的名字
        # members_type, private_members = get_members_info(file)
        # 获取成员函数的参数类型，成员函数的参数名，成员函数的返回值类型
        # public_methods, methods_params, methods_return, methods_params_type, methods_params_name, methods_return_type = get_methods_info(file)
        
        # 遍历类名
        for i in range(len(class_name)):
            # 添加表格行
            row_cells = table.add_row().cells
            # 设置表格内容
            row_cells[0].text = class_name[i]
        for i in range(len(parent_class_name)):
            row_cells[1].text = parent_class_name[i]
        for i in range(len(class_parent_type)):
            row_cells[2].text = class_parent_type[i]
        
        body_cells = table.rows[1:]
        for i in range(len(table.rows) - 1):
            for j in range(len(table.columns)):
                # body_cells[i].cells[j].paragraphs[0].runs[0].font.size = Pt(10)
                body_cells[i].cells[j].paragraphs[0].runs[0].font.bold = False
                # body_cells[i].cells[j].paragraphs[0].runs[0].font.color.rgb = RGBColor(0, 0, 0)


        # 清除table变量
        # del table
        # 换行
        doc.add_paragraph()

    # 保存word文档
    doc.save('test.docx')

    # 打开test.docx
    os.system("test.docx")

if __name__ == '__main__':
    
    main()